import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.File;

public class MergeDep {

    public static void main(String[] args) throws Exception {
        File parentPomFile = new File("../pom.xml");  // parent pom
        File targetPomFile = new File("pom.xml");     // 当前项目 pom

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setIgnoringElementContentWhitespace(true); // 保留原有结构
        DocumentBuilder builder = factory.newDocumentBuilder();

        Document parentDoc = builder.parse(parentPomFile);
        Document targetDoc = builder.parse(targetPomFile);

        Element targetRoot = targetDoc.getDocumentElement();

        // === 1. 处理 properties ===
        NodeList parentPropsList = parentDoc.getElementsByTagName("properties");
        if (parentPropsList.getLength() > 0) {
            Node parentProps = parentPropsList.item(0);

            // 删除 target 里原有的 properties
            NodeList targetPropsList = targetDoc.getElementsByTagName("properties");
            for (int i = 0; i < targetPropsList.getLength(); i++) {
                targetRoot.removeChild(targetPropsList.item(i));
            }

            // 复制 parent 的 properties 到 target
            Node importedProps = targetDoc.importNode(parentProps, true);
            // 插在 dependencyManagement 前（如果存在）
            NodeList dmList = targetDoc.getElementsByTagName("dependencyManagement");
            if (dmList.getLength() > 0) {
                targetRoot.insertBefore(importedProps, dmList.item(0));
            } else {
                targetRoot.appendChild(importedProps);
            }
        }

        // === 2. 处理 dependencyManagement ===
        NodeList parentDepMgmtList = parentDoc.getElementsByTagName("dependencyManagement");
        if (parentDepMgmtList.getLength() > 0) {
            Node parentDepMgmt = parentDepMgmtList.item(0);

            // 删除 target 里原有的 dependencyManagement
            NodeList targetDepMgmtList = targetDoc.getElementsByTagName("dependencyManagement");
            for (int i = 0; i < targetDepMgmtList.getLength(); i++) {
                targetRoot.removeChild(targetDepMgmtList.item(i));
            }

            // 复制 parent 的 dependencyManagement 到 target，放在最后
            Node importedDepMgmt = targetDoc.importNode(parentDepMgmt, true);
            targetRoot.appendChild(importedDepMgmt);
        }

        // === 3. 输出 ===
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        // 让缩进更紧凑（避免过度换行）
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");

        transformer.transform(new DOMSource(targetDoc), new StreamResult(targetPomFile));

        System.out.println("✅ 已合并 parent 的 <properties> 和 <dependencyManagement> 到 " + targetPomFile.getName());
    }
}
